package com.jp.knowledge.adapter.skill;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.SkillModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/4/20.
 */

public class SkillHeadAdapter extends RecycleVAdapter<SkillModel.CategoryBean> {

    private int selectPosition;

    public SkillHeadAdapter(Context context, @NonNull List<SkillModel.CategoryBean> list) {
        super(context, list);
        selectPosition = 0;
    }

    public void setSelectPosition(int position){
        this.selectPosition = position;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_skill_head;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        SkillModel.CategoryBean data = getData(position);
        ImageView icon = holder.getImageView(R.id.skill_head_icon);
        ImageLoadUtil.load(mContext, data.getIcon(), icon);
        icon.setBackground(getIconBg(data.getColor()));
        TextView title = holder.getTextView(R.id.skill_head_title);
        title.setText(data.getName());
        if(selectPosition == position){
            title.setTextColor(mContext.getResources().getColor(R.color.theme_color));
            holder.get(R.id.skill_head_line).setVisibility(View.VISIBLE);
        } else{
            title.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
            holder.get(R.id.skill_head_line).setVisibility(View.INVISIBLE);
        }
    }

    private Drawable getIconBg(final String color){
        final int bgColor;
        try {
            bgColor = Color.parseColor(color);
        } catch (Exception e){
            return null;
        }
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        shapeDrawable.setShape(new Shape() {
            @Override
            public void draw(Canvas canvas, Paint paint) {
                paint.setColor(bgColor);
                canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2, paint);
            }
        });
        return shapeDrawable;
    }
}
